home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Mark Pilgrim / Go Sit In The Corner 1.0 / source / init code / prefs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-30  |  5.0 KB  |  208 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        prefs.c
  4.  
  5. Purpose:    This module handles getting stuff from the prefs file,
  6.             like the current module name, and then saving other stuff,
  7.             like where the current module is installed in memory.
  8.  
  9.  
  10. Go Sit In The Corner -=- not you, just the cursor
  11. Copyright ©1994, Mark Pilgrim
  12.  
  13. This program is free software; you can redistribute it and/or modify
  14. it under the terms of the GNU General Public License as published by
  15. the Free Software Foundation; either version 2 of the License, or
  16. (at your option) any later version.
  17.  
  18. This program is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. GNU General Public License for more details.
  22.  
  23. You should have received a copy of the GNU General Public License
  24. along with this program in a file named "GNU General Public License".
  25. If not, write to the Free Software Foundation, 675 Mass Ave,
  26. Cambridge, MA 02139, USA.
  27.  
  28. \**********************************************************************/
  29.  
  30. #include "structs.h"
  31. #include "prefs.h"
  32. #include "GestaltEQU.h"
  33. #include "Folders.h"
  34.  
  35. int PreferencesInit(PrefHandle prefsHandle)
  36. {
  37.     int            prefsFileID;
  38.     int            err;
  39.     
  40.     err=OpenPrefsFile(&prefsFileID, prefsHandle);
  41.     if (err!=prefs_allsWell)
  42.     {
  43.         if ((err==prefs_diskReadErr) || (err==prefs_diskWriteErr) || (err==prefs_virginErr))
  44.             ClosePrefsFile(prefsFileID);
  45.         return err;
  46.     }
  47.     
  48.     GetNextPrefs(prefsFileID, prefsHandle);
  49.     if (err!=prefs_allsWell)
  50.     {
  51.         ClosePrefsFile(prefsFileID);
  52.         return err;
  53.     }
  54.     
  55.     ClosePrefsFile(prefsFileID);
  56.     
  57.     return prefs_allsWell;
  58. }
  59.  
  60. int OpenPrefsFile(int *prefsFileID, PrefHandle prefsHandle)
  61. {
  62.     int                thisFile;
  63.     OSErr            isHuman;
  64.     int                vRefNum;
  65.     long            dirID;
  66.     FSSpec            prefsFile;
  67.     FInfo            prefsInfo;
  68.     int                temp;
  69.     long            count;
  70.     Boolean            newPrefs;
  71.     int                err;
  72.     unsigned char    *name=PREFS_FILE_NAME;
  73.     long            gestalt_temp;
  74.     Boolean            gHasFSSpecs;
  75.     
  76.     isHuman = Gestalt(gestaltFSAttr, &gestalt_temp);
  77.     gHasFSSpecs=((isHuman==noErr) && (gestalt_temp & (1 << gestaltHasFSSpecCalls)));
  78.     
  79.     newPrefs=FALSE;
  80.     isHuman=FindFolder(kOnSystemDisk, 'pref', kCreateFolder, &vRefNum, &dirID);
  81.     
  82.     if (isHuman!=noErr)
  83.         return prefs_cantOpenPrefsErr;
  84.  
  85.     if (gHasFSSpecs)
  86.     {
  87.         isHuman=FSMakeFSSpec(vRefNum, dirID, name, &prefsFile);
  88.         if (isHuman!=noErr)
  89.         {
  90.             if (isHuman==fnfErr)
  91.             {
  92.                 isHuman=FSpCreate(&prefsFile, CREATOR, PREFS_TYPE, 0);
  93.                 if (isHuman!=noErr)
  94.                     return prefs_cantCreatePrefsErr;
  95.                 newPrefs=TRUE;
  96.             }
  97.             else return prefs_cantOpenPrefsErr;
  98.         }
  99.         isHuman=FSpOpenDF(&prefsFile, fsRdWrPerm, &thisFile);
  100.         *prefsFileID=thisFile;
  101.         if (isHuman!=noErr)
  102.             return prefs_cantOpenPrefsErr;
  103.     }
  104.     else
  105.     {
  106.         isHuman=HOpen(vRefNum, dirID, name, fsRdWrPerm, &thisFile);
  107.         *prefsFileID=thisFile;
  108.         if (isHuman!=noErr)
  109.         {
  110.             if (isHuman==fnfErr)
  111.             {
  112.                 isHuman=HCreate(vRefNum, dirID, name, CREATOR, PREFS_TYPE);
  113.                 if (isHuman!=noErr)
  114.                     return prefs_cantCreatePrefsErr;
  115.                 prefsInfo.fdType=PREFS_TYPE;
  116.                 prefsInfo.fdCreator=CREATOR;
  117.                 prefsInfo.fdFlags=0;
  118.                 prefsInfo.fdLocation.h=prefsInfo.fdLocation.v=0;
  119.                 prefsInfo.fdFldr=0;
  120.                 isHuman=HSetFInfo(vRefNum, dirID, name, &prefsInfo);
  121.                 if (isHuman!=noErr)
  122.                     return prefs_cantCreatePrefsErr;
  123.                 isHuman=HOpen(vRefNum, dirID, name, fsRdWrPerm, &thisFile);
  124.                 *prefsFileID=thisFile;
  125.                 if (isHuman!=noErr)
  126.                     return prefs_cantOpenPrefsErr;
  127.                 newPrefs=TRUE;
  128.             }
  129.             else return prefs_cantOpenPrefsErr;
  130.         }
  131.     }
  132.     if (newPrefs)
  133.     {
  134.         return Virgin(*prefsFileID, prefsHandle);
  135.     }
  136.     
  137.     return prefs_allsWell;
  138. }
  139.  
  140. void ClosePrefsFile(int prefsFileID)
  141. {
  142.     FSClose(prefsFileID);
  143.     FlushVol(0L, kOnSystemDisk);
  144. }
  145.  
  146. int GetNextPrefs(int prefsFileID, PrefHandle prefsHandle)
  147. {
  148.     OSErr        isHuman;
  149.     long        count;
  150.     
  151.     count=GetHandleSize(prefsHandle);
  152.     isHuman=FSRead(prefsFileID, &count, *prefsHandle);
  153.     if (isHuman!=noErr)
  154.         return prefs_diskReadErr;
  155.     
  156.     return prefs_allsWell;
  157. }
  158.  
  159. int SavePrefs(int prefsFileID, PrefHandle prefsHandle)
  160. {
  161.     long        oldEOF;
  162.     OSErr        isHuman;
  163.     long        count;
  164.     
  165.     isHuman=SetEOF(prefsFileID, GetHandleSize(prefsHandle));
  166.     if (isHuman!=noErr)
  167.         return prefs_diskWriteErr;
  168.  
  169.     SetFPos(prefsFileID, 1, 0L);
  170.     count=GetHandleSize(prefsHandle);
  171.     isHuman=FSWrite(prefsFileID, &count, *prefsHandle);
  172.     if (isHuman!=noErr)
  173.         return prefs_diskWriteErr;
  174.     
  175.     return prefs_allsWell;
  176. }
  177.  
  178. int Virgin(int prefsFileID, PrefHandle prefsHandle)
  179. {
  180.     int            err;
  181.     
  182.     DefaultPrefs(prefsHandle);
  183.     err=SavePrefs(prefsFileID, prefsHandle);
  184.     
  185.     return (err==prefs_allsWell) ? prefs_virginErr : err;
  186. }
  187.  
  188. void DefaultPrefs(PrefHandle prefsHandle)
  189. {
  190.     int                i;
  191.     
  192.     (**prefsHandle).showIcon=0xFF;
  193.     (**prefsHandle).isOn=0xFF;
  194.     (**prefsHandle).ourCodePtr=0L;
  195.     (**prefsHandle).ourVBLPtr=0L;
  196.     (**prefsHandle).whichCorner=0x00;    /* top-left corner */
  197.     (**prefsHandle).always=0x00;        /* only if no mouse movement */
  198. }
  199.  
  200. void SaveThePrefs(PrefHandle prefsHandle)
  201. {
  202.     int                prefsFileID;
  203.     
  204.     OpenPrefsFile(&prefsFileID, prefsHandle);
  205.     SavePrefs(prefsFileID, prefsHandle);
  206.     ClosePrefsFile(prefsFileID);
  207. }
  208.